import pymysql
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QRegExp
from PyQt5.QtGui import QFont, QIcon, QRegExpValidator, QPalette
from utils.custom_widgets import MyLineEdit

class RegisterPage(QDialog):
    def __init__(self):
        super(RegisterPage, self).__init__()
        self.setWindowTitle('用户注册')
        self.setWindowIcon(QIcon("./gui_image/gui_Icon.png"))
        self.move(900, 400)
        self.setFixedSize(450, 250)
        self.init_ui()

    def init_ui(self):
        self.label_0 = QLabel('欢迎注册')
        self.label_0.setFont(QFont('宋体', 12, QFont.Bold))
        self.label_0.setAlignment(Qt.AlignCenter)
        
        self.id_label = QLabel('用户ID：')
        self.password1_label = QLabel('用户密码：')
        self.password2_label = QLabel('重复密码：')
        self.name_label = QLabel('用户姓名：')
        self.gender_label = QLabel('性别：')
        
        self.id_line = QLineEdit()
        self.password1_line = QLineEdit()
        self.password2_line = MyLineEdit()
        self.name_line = QLineEdit()
        self.name_line_En = QLineEdit()
        
        self.male_button = QRadioButton('男')
        self.female_button = QRadioButton('女')
        
        self.line_init()
        self.modify_widget()
        
        self.register_button = QPushButton('注册')
        self.cancel_button = QPushButton('取消')
        
        self.pushbutton_init()
        self.layout_init()

    def line_init(self):
        self.id_line.textChanged.connect(self.check_input)
        self.password1_line.textChanged.connect(self.check_input)
        self.password2_line.textChanged.connect(self.check_input)
        self.name_line.textChanged.connect(self.check_input)
        self.name_line_En.textChanged.connect(self.check_input)
        self.male_button.clicked.connect(self.check_input)
        self.female_button.clicked.connect(self.check_input)
        
        self.password1_line.setEchoMode(QLineEdit.PasswordEchoOnEdit)
        self.password2_line.setEchoMode(QLineEdit.PasswordEchoOnEdit)
        
        self.id_line.setPlaceholderText('输入用户数字工号，不可为中文或特殊字符。')
        self.password1_line.setPlaceholderText('密码为6到10位数字字母，首字母必须为大写。')
        self.password2_line.setPlaceholderText('请再次确认密码！')
        self.name_line.setPlaceholderText('中文名!')
        self.name_line_En.setPlaceholderText('英文名!')
        
        id_RegExp = QRegExp("[0-9A-Za-z]*")
        id_validator = QRegExpValidator(id_RegExp)
        self.id_line.setValidator(id_validator)
        
        password_val = QRegExpValidator(QRegExp("^[A-Z][0-9A-Za-z]{10}$"))
        self.password1_line.setValidator(password_val)
        self.password2_line.setValidator(password_val)
        
        self.password2_line.focus_out.connect(self.check_password)

    def modify_widget(self):
        self.name_label.setMinimumWidth(60)
        self.name_line.setMaximumWidth(80)
        self.name_line_En.setMaximumWidth(80)

    def pushbutton_init(self):
        self.register_button.setEnabled(False)
        self.register_button.clicked.connect(self.register_func)
        self.cancel_button.clicked.connect(self.cancel_func)

    def layout_init(self):
        # 布局代码保持原样...
        pass

    def check_input(self):
        gender = self.male_button.isChecked() or self.female_button.isChecked()
        if (self.id_line.text() and self.password1_line.text() and
            self.password2_line.text() and self.name_line.text() and 
            self.name_line_En.text() and gender):
            self.register_button.setEnabled(True)
        else:
            self.register_button.setEnabled(False)

    def cancel_func(self):
        if (self.id_line.text() or self.password1_line.text() or
            self.password2_line.text()):
            choice = QMessageBox.information(self,'提示','是否取消注册？',
                                    QMessageBox.Yes | QMessageBox.No)
            if choice == QMessageBox.Yes:
                self.close()
        else:
            self.close()

    def check_password(self):
        password_1 = self.password1_line.text()
        password_2 = self.password2_line.text()
        if len(password_1) < 6:
            QMessageBox.warning(self,'警告','密码位数小于6')
            self.password1_line.setText('')
            self.password2_line.setText('')
        else:
            if password_1 != password_2:
                QMessageBox.warning(self,'警告','两次密码输入结果不一致！')
                self.password1_line.setText('')
                self.password2_line.setText('')

    def register_func(self):
        # 注册逻辑保持原样...
        pass